; (function (window, undefined) {
	function init(el) {
		// this 全等于 实例化对象

		// 1 获取所有的数据
		let els = document.querySelectorAll(el)
		// 2 遍历数据，将js dom绑定到jq对象上
		for (let i = 0; i < els.length; i++) {
			// i 索引
			// els[i] 获取js标签对象
			this[i] = els[i]
		}

		this.length = els.length

		// 上述代码优化：
		// let arr = []
		// arr.push.apply(this, els);
	}
	// 正常：1000个 window*6 = 6000个字符
	// 压缩：window形参改成一个字符
	let jQuery = function (el) {
		// element
		return new init(el)
	}

	// jQuery.prototype = {
	init.prototype = {
		css(cssAttr, cssVal) {
			// console.log(1, this);   // === 构造函数中this == 实例化
			// console.log(this[0])
			// this[0].style.cssAttr = cssVal
			// this[1].style.cssAttr = cssVal
			// this[2].style.cssAttr = cssVal
			for (let i = 0; i < this.length; i++) {
				// this[2].style.cssAttr = cssVal
				// this[i].style.cssAttr = cssVal // 当对象的键是变量时 用数组语法
				this[i].style[cssAttr] = cssVal // 当对象的键是变量时 用数组语法
			}
			return this
		},

		ajax(
			method = "get",
			url = "",
			params = "",
			headers = {},
			dataReceivedType = "JSON",
			cb = function () { }
		) {
			const xhr = new XMLHttpRequest()
			xhr.onreadystatechange = function () {
				if (xhr.readyState === 4) {
					if (xhr.status === 200) {
						let res = xhr.responseText
						switch (dataReceivedType) {
							case "JSON":
								res = JSON.parse(res)
								cb(res)
								break
							default:
								alert(`暂时不支持：${dataReceivedType}数据格式`)
						}
					} else {
						alert("error in xhr:", xhr)
						return "error in xhr"
					}
				}
			}

			// 获取参数类型  使用函数立即执行，如果复制给变量，省略前面的;
			let paramType = (function (obj) {
				return Object.prototype.toString.call(obj).toLowerCase().slice(8, -1)
			})(params)
			//也等于下面的简写
			// paramsType = Object.prototype.toString
			//   .call(params)
			//   .toLowerCase()
			//   .slice(8, -1)

			// 最终的 字符串类型的请求参数
			let requestParamsString = ""
			switch (paramType) {
				case "string":
					requestParamsString = params
					break

				case "object":
					requestParamsString = (function (params = {}) {
						let s = ""
						for (let key in params) {
							s += `${key}=${params[key]}&`
						}
						s = s.slice(0, -1)
						return s
					})(params)
					break

				default:
			}

			//分类处理
			switch (method) {
				case "get":
					xhr.open("get", `${url}?${requestParamsString}`)
					for (let key in headers) {
						xhr.setRequestHeader(key, headers[key])
					}
					xhr.send(null)
					break
				case "post":
					xhr.open("post", url)
					xhr.setRequestHeader(
						"content-type",
						"application/x-www-form-urlencoded"
					)
					for (let key in headers) {
						xhr.setRequestHeader(key, headers[key])
					}
					xhr.send(requestParamsString)
					break
				default:
			}
		},

		attr(key = "", value) {
			for (let i = 0; i < this.length; i++) {
				this[i].setAttribute(key, value)
			}
			return this
		},

		addClass(newClassName = "") {
			for (let i = 0; i < this.length; i++) {
				if (this[i].className) this[i].className += " " + newClassName
				else this[i].className = newClassName
			}
			return this
		},
	}

	window.jQuery = window.$ = jQuery
})(window)